package com.it.myd.controller.main.module;

import com.it.myd.bean.box.world.Item;
import com.it.myd.bean.box.world.Material;
import com.it.myd.bean.box.world.Player;
import com.it.myd.bean.box.world.Resource;
import com.it.myd.bean.main.ReturnValue;
import com.it.myd.bean.main.User;
import com.it.myd.help.ItemLoader;
import com.it.myd.service.main.module.world.ItemService;
import com.it.myd.service.main.module.world.MaterialServiceImpl;
import com.it.myd.service.main.module.world.PlayerService;
import com.it.myd.service.main.module.world.ResourceService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/module/world")
public class WorldController {
    @Autowired
    private ResourceService resourceService;
    @Autowired
    private MaterialServiceImpl materialService;
    @Autowired
    private PlayerService playerService;
    @Autowired
    private ItemService itemService;

    @RequestMapping("upset")
    @ResponseBody
    public ReturnValue upset(HttpServletRequest request){
        ReturnValue ret = new ReturnValue();
        HttpSession session = request.getSession();
        String res_name = request.getParameter("res_name");
        User user = (User) session.getAttribute("user");
        List<Resource> reslist = resourceService.getReslist();
        Boolean flag = false;
        if(reslist != null){
            for (Resource resource: reslist) {
                if(resource.getRes_Name().equals(res_name)){
                    flag = true;
                    if(resource.getUseUser()==null){
                        for (Resource res: reslist) {
                            if(res.getUseUser() != null && res.getUseUser().getUser_id() == user.getUser_id()){
                                res.setUseUser(null);
                                res.setRes_State("1");
                                resourceService.updateResource(res);
                            }
                        }
                        resource.setUseUser(user);
                        resource.setRes_State("2");
                        playerService.stopPlayer(user);
                        playerService.changerState(user,3);
                        resourceService.updateResource(resource);
                        ret.isSuccess();
                    }else{
                        if(resource.getUseUser().getUser_id()== user.getUser_id()){
                            ret.isFail("已经在使用中");
                        }else{
                            ret.isFail("已存在使用者"+resource.getUseUser().getUser_name());
                        }
                    }
                    break;
                }
            }
            if(!flag){
                ret.isFail("资源不存在");
            }
        }else{
            ret.isFail("资源不存在");
        }
        resourceService.setReslist(reslist);
        return ret;
    }

    @RequestMapping("standUp")
    @ResponseBody
    public ReturnValue standUp(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        ReturnValue ret = new ReturnValue();
        List<Resource> reslist = resourceService.getReslist();
        boolean flag = false;
        if(reslist!= null && reslist.size() > 0){
            for (Resource resource: reslist) {
                if(resource.getUseUser()!=null && resource.getUseUser().getUser_id() == user.getUser_id()){
                    playerService.outPlayer(resource);
                    resource.setUseUser(null);
                    resource.setRes_State("1");
                    resourceService.updateResource(resource);
                    flag = true;
                }
            }
        }
        if(flag){
            ret.isSuccess();
        }else{
            ret.isFail();
        }
        return ret;
    }

    @RequestMapping("playerMove")
    @ResponseBody
    public ReturnValue playerMove(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        ReturnValue ret = new ReturnValue();
        String toLeft = request.getParameter("toLeft");
        String toTop = request.getParameter("toTop");
        standUp(request);
        playerService.playerMove(user,Double.valueOf(toLeft),Double.valueOf(toTop));
        ret.isSuccess();
        return ret;
    }

    @RequestMapping("getres")
    @ResponseBody
    public ReturnValue<Resource> getRes(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        ReturnValue ret = new ReturnValue();
        List<Resource> reslist = resourceService.getReslist();
        Map<String,List<Material>> map = materialService.getMaterial();
        List<Material> list = map.get(String.valueOf(user.getUser_id()));
        List<Player> list1 = playerService.getPlayerList();
        Map<String,List<Item>> map1  = itemService.getuserItemList();
        List<Item> list2 = map1.get(String.valueOf(user.getUser_id()));
        ret.setRetList(reslist);
        ret.setObjList(list);
        ret.setObjList1(list1);
        ret.setObjList2(list2);
        return ret;
    }

    @RequestMapping("createItem")
    @ResponseBody
    public ReturnValue createItem(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        ReturnValue ret = new ReturnValue();
        String id = request.getParameter("id");
        String count = request.getParameter("count");
        ItemLoader itemLoader = new ItemLoader();
        Item item = itemLoader.getItem(id);
        item.setCount(Integer.parseInt(count));
        if(item != null){
            boolean flag = materialService.chickMaterial(user,item);
            if(flag){
                List<Item> items = new ArrayList<Item>();
                items.add(item);
                itemService.adduserCreatItemList(user,items);
                playerService.changerState(user,4);
                ret.isSuccess();
            }else{
                ret.isFail("材料不足!");
            }
        }else{
            ret.isFail("未找到对应制造物!");
        }
        return ret;
    }

    @RequestMapping("useItem")
    @ResponseBody
    public ReturnValue useItem(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        ReturnValue ret = new ReturnValue();
        String type = request.getParameter("type");
        String count = request.getParameter("count");
        String code = request.getParameter("code");
        if("1".equals(type)){//装备类
            Item item = itemService.getItemByCode(user,code);
            itemService.adduserUseItem(user,item);
        }
        ret.isSuccess();
        return ret;
    }

    @RequestMapping("removeItem")
    @ResponseBody
    public ReturnValue removeItem(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        ReturnValue ret = new ReturnValue();
        String type = request.getParameter("type");
        String code = request.getParameter("code");
        if("1".equals(type)){//装备类
            itemService.removeuserUseItem(user,code);
        }
        ret.isSuccess();
        return ret;
    }

    @RequestMapping("deleteItem")
    @ResponseBody
    public ReturnValue deleteItem(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        ReturnValue ret = new ReturnValue();
        String type = request.getParameter("type");
        String code = request.getParameter("code");
        if("1".equals(type)){//装备类
            itemService.deleteItem(user,code);
        }
        ret.isSuccess();
        return ret;
    }


}
